package com.douban.crawl.crawler;

import com.douban.crawl.db.BookDao;
import com.douban.crawl.db.MovieDao;
import com.douban.crawl.db.MusicDao;
import com.douban.crawl.model.Book;
import com.douban.crawl.model.Movie;
import com.douban.crawl.model.Music;
import com.douban.crawl.utils.CommonUtil;

import java.util.List;
import java.util.concurrent.TimeUnit;

public class ImageCrawler {

    public static void main(String[] args) throws InterruptedException {
        MovieDao movieDao = new MovieDao();
        MusicDao musicDao = new MusicDao();
        BookDao bookDao = new BookDao();

        List<Movie> movieList = movieDao.getList();
        List<Music> musicList = musicDao.getList();
        List<Book> bookList = bookDao.getList();

        System.out.println("正在下载图片。。。。。。");
        for (Movie movie : movieList) {
            if (movie.getImg_name() != null && !"".equals(movie.getImg_name())) {
                continue;
            }
            CommonUtil.downloadImg(movie.getMovie_poster_url());
            String fileName = CommonUtil.getFileName(movie.getMovie_poster_url());
            movie.setImg_name(fileName);
            movieDao.update(movie);

            TimeUnit.SECONDS.sleep(2);
        }

        for (Music music : musicList) {
            if (music.getImg_name() != null && !"".equals(music.getImg_name())) {
                continue;
            }
            CommonUtil.downloadImg(music.getMusic_img_url());
            String fileName = CommonUtil.getFileName(music.getMusic_img_url());
            music.setImg_name(fileName);
            musicDao.update(music);

            TimeUnit.SECONDS.sleep(2);
        }

        for (Book book : bookList) {
            if (book.getImg_name() != null && !"".equals(book.getImg_name())) {
                continue;
            }
            CommonUtil.downloadImg(book.getBook_img_url());
            String fileName = CommonUtil.getFileName(book.getBook_img_url());
            book.setImg_name(fileName);
            bookDao.update(book);

            TimeUnit.SECONDS.sleep(2);
        }

        System.out.println("图片下载完毕！");
    }
}
